<?php

namespace App\Models;

use App\Models\Rooms\RoomsGoodsBuy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Laravel\Passport\HasApiTokens;

class Members extends Authenticatable
{
    use HasApiTokens,Notifiable;

    protected $table = "members";
    protected $fillable = [
        'username','password','mobile','nickname','realname','idcrad','referee_phone','pay_password','invite','pid','is_house_owner','bean','earnings','fruit'
    ];

    protected $hidden = ['password','pay_password'];

    public function findForPassport($username)
    {
        return $this->where(['username'=>$username])->orWhere('mobile',$username)->first();
    }

    public function validateForPassportPasswordGrant($password)
    {
        return Hash::make($password);
    }

    public function getUsernameAttribute($value)
    {
        return is_null($value) ? '' : $value;
    }

    public function getRealnameAttribute($value)
    {
        return is_null($value) ? '' : $value;
    }

    public function getIdcradAttribute($value)
    {
        return is_null($value) ? '' : $value;
    }

    public function getRefereePhoneAttribute($value)
    {
        return is_null($value) ? '' : $value;
    }

    public function getNicknameAttribute($value)
    {
        return is_null($value) ? '该用户尚未设置昵称' : $value;
    }

    public function getAvatarAttribute($value)
    {
        return is_null($value) ? 'http://api.seclucky.com/images/avatar.png' : $value;
    }

    public function room(){
        return $this->hasOne(Rooms::class, 'member_id', 'id');
    }

    public function roomsGoods(){
        return $this->hasOne(RoomsGoodsBuy::class, 'member_id', 'id');
    }

    public function buysAmount(){
        return $this->hasOne(RoomsGoodsBuy::class, 'member_id', 'id')->withDefault()->groupBy('member_id');
    }

}
